<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="isDialog.visible"
    title="账户充值"
    top="7vh"
    width="600px"
  >
    <el-form
      ref="submitFormRef"
      :model="submitForm"
      :rules="submitFormRules"
      label-position="right"
      label-width="auto"
    >
      <el-form-item label="账户" prop="account">
        <el-select
          v-model="submitForm.account"
          clearable
          placeholder="请选择账户"
        >
          <el-option
            v-for="item in filterItem.account"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="金额" prop="amount">
        <el-input
          v-model="submitForm.amount"
          clearable
          placeholder="请输入金额"
          type="number"
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { recharge } from "@/api/rechargeRecord";

export default {
  name: "Recharge",
  props: {
    info: {
      type: Object,
      require: true,
    },
    account: {
      type: Array,
    },
    type: {
      type: String,
    },
  },
  data() {
    return {
      // 表单提交
      submitForm: {
        user_type: "provider",
        user_id: "",
        account: "",
        amount: "",
      },
      // 表单验证
      submitFormRules: {
        account: [{ required: true, message: "请选择账户", trigger: "blur" }],
        amount: [{ required: true, message: "请输入金额", trigger: "blur" }],
      },

      // 筛选项
      filterItem: {
        account: [
          {
            label: "积分",
            value: "score",
          },
          {
            label: "能量",
            value: "energy",
          },
          {
            label: "保证金",
            value: "bond",
          },
        ],
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
      // 是否弹窗
      isDialog: {
        visible: true,
      },
    };
  },
  created() {
    this.submitForm.user_id = this.info.provider_id;
    this.submitForm.account = this.type;
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$refs.submitFormRef.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.isLoading.submit = true;
        recharge(this.submitForm)
          .then((err) => {
            this.isLoading.submit = false;
            this.$emit("close");
          })
          .catch(() => {})
          .finally(() => {
            this.isLoading.submit = false;
          });
      });
    },
    // 关闭
    handleClose() {
      this.$emit("close", "cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea textarea {
  font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
</style>
